package com.sky.service.Impl;

import com.sky.constant.MessageConstant;
import com.sky.exception.CustomException;
import com.sky.mapper.AdminMapper;
import com.sky.model.dto.AdminDTO;
import com.sky.model.dto.AdminLoginDTO;
import com.sky.model.entity.Admin;
import com.sky.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 管理员登录
     * @param adminLoginDTO
     * @return
     */
    @Override
    public Admin login(AdminLoginDTO adminLoginDTO) {
        String username = adminLoginDTO.getUsername();
        String password = adminLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Admin admin = adminMapper.findUser(username, password);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (admin == null) {
            //账号不存在
            throw new CustomException(MessageConstant.USERNAME_ERROR);
        }

        //密码比对
        //对前端传送过来的明文密码进行MD5加密处理
       /* password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        if (!password.equals(admin.getPassword())) {
            //密码错误
            throw new CustomException(MessageConstant.PASSWORD_ERROR);
        }*/


        //3、返回实体对象
        return admin;
    }

    /**
     * 修改个人信息
     * @param adminDTO
     */
    @Override
    public void update(AdminDTO adminDTO) {
        adminMapper.update(adminDTO);
    }
}

